//
//  TSAlertView.m
//
//  Created by Nick Hodapp aka Tom Swift on 1/19/11.
//

#import "TSAlertView.h"
#import <QuartzCore/QuartzCore.h>
#import "TSAlertViewManager.h"
#import "AppDelegate.h"

@implementation  TSAlertOverlayWindow
@synthesize oldKeyWindow;

- (void) makeKeyAndVisible
{
	self.oldKeyWindow = [[UIApplication sharedApplication] keyWindow];
	self.windowLevel = UIWindowLevelAlert;
	[super makeKeyAndVisible];
}

- (void) resignKeyWindow
{
	[super resignKeyWindow];
	[self.oldKeyWindow makeKeyWindow];
}

- (void) drawRect: (CGRect) rect
{
	// render the radial gradient behind the alertview
	
	CGFloat width			= self.frame.size.width;
	CGFloat height			= self.frame.size.height;
	CGFloat locations[3]	= { 0.0, 0.5, 1.0 	};
	CGFloat components[12]	= {	1, 1, 1, 0.5,
		0, 0, 0, 0.5,
		0, 0, 0, 0.7	};
	
	CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
	CGGradientRef backgroundGradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 3);
	CGColorSpaceRelease(colorspace);
	
	CGContextDrawRadialGradient(UIGraphicsGetCurrentContext(), 
								backgroundGradient, 
								CGPointMake(width/2, height/2), 0,
								CGPointMake(width/2, height/2), width,
								0);
	
	CGGradientRelease(backgroundGradient);
}

- (void) dealloc
{
	self.oldKeyWindow = nil;
	
	NSLog( @"TSAlertView: TSAlertOverlayWindow dealloc" );
	
	[super dealloc];
}

@end

@interface TSAlertView (private)
@property (nonatomic, readonly) NSMutableArray* buttons;
@property (nonatomic, readonly) UILabel* titleLabel;
@property (nonatomic, readonly) UILabel* messageLabel;

- (void) TSAlertView_commonInit;
- (void) releaseWindow: (int) buttonIndex;
- (void) pulse;
- (CGSize) titleLabelSize;
- (CGSize) imageOnAlertSize;
- (CGSize) messageLabelSize;
- (CGSize) buttonsAreaSize_Stacked;
- (CGSize) buttonsAreaSize_SideBySide;
- (CGSize) recalcSizeAndLayout: (BOOL) layout;
@end


@implementation TSAlertViewController
/*
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
	return YES;
}
 */
 
/*
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
	TSAlertView* av = [self.view.subviews lastObject];
	if (!av || ![av isKindOfClass:[TSAlertView class]])
		return;
	// resize the alertview if it wants to make use of any extra space (or needs to contract)
	[UIView animateWithDuration:duration 
					 animations:^{
						 [av sizeToFit];
						 av.center = CGPointMake( CGRectGetMidX( self.view.bounds ), CGRectGetMidY( self.view.bounds ) );;
						 av.frame = CGRectIntegral( av.frame );
					 }];
}
 */

- (void) dealloc
{
	NSLog( @"TSAlertView: TSAlertViewController dealloc" );
	[super dealloc];
}

@end

@implementation TSAlertView

@synthesize delegate;
@synthesize cancelButtonIndex;
@synthesize firstOtherButtonIndex;
@synthesize buttonLayout;
@synthesize width;
@synthesize maxHeight;
@synthesize isDismissByClick;
@synthesize backgroundImage = _backgroundImage;
@synthesize alertImage = _alertImage;
@synthesize style;

const CGFloat kTSAlertView_LeftMargin	= 10.0;
const CGFloat kTSAlertView_TopMargin	= 16.0;
const CGFloat kTSAlertView_BottomMargin = 15.0;
const CGFloat kTSAlertView_RowMargin	= 5.0;
const CGFloat kTSAlertView_ColumnMargin = 10.0;

- (id) init 
{
	if ( ( self = [super init] ) )
	{
		[self TSAlertView_commonInit];
	}
//    NSLog(@"init %@", self);
	return self;
}

- (id) initWithFrame:(CGRect)frame
{
	if ( ( self = [super initWithFrame: frame] ) )
	{
		[self TSAlertView_commonInit];
		
		if ( !CGRectIsEmpty( frame ) )
		{
			width = frame.size.width;
			maxHeight = frame.size.height;
		}
	}
//    NSLog(@"initWithFrame %@", self);
	return self;
}

- (id) initWithTitle: (NSString *) t 
             message: (NSString *) m 
            delegate: (id) d 
   cancelButtonTitle: (NSString *) cancelButtonTitle 
   otherButtonTitles: (NSString *) otherButtonTitles, ...
{
	if ( (self = [super init] ) ) // will call into initWithFrame, thus TSAlertView_commonInit is called
	{
		self.title = t;
		self.message = m;
		self.delegate = d;
		
		if ( nil != cancelButtonTitle )
		{
			[self addButtonWithTitle: cancelButtonTitle ];
			self.cancelButtonIndex = 0;
		}
		
		if ( nil != otherButtonTitles )
		{
			firstOtherButtonIndex = [self.buttons count];
			[self addButtonWithTitle: otherButtonTitles ];
			
			va_list args;
			va_start(args, otherButtonTitles);
			
			id arg;
			while ( nil != ( arg = va_arg( args, id ) ) ) 
			{
				if ( ![arg isKindOfClass: [NSString class] ] )
					return nil;
				
				[self addButtonWithTitle: (NSString*)arg ];
			}
		}
	}
	
	return self;
}

- (CGSize) sizeThatFits: (CGSize) unused 
{
	CGSize s = [self recalcSizeAndLayout: NO];
	return s;
}

- (void) layoutSubviews
{
    //NSLog(@" alertMessate: %@", self.message);
	[self recalcSizeAndLayout: YES];
}

- (void) drawRect:(CGRect)rect
{
	[self.backgroundImage drawInRect: rect];
}

- (void)dealloc 
{
    [[NSNotificationCenter defaultCenter] removeObserver: self ];
	[_backgroundImage release];
    [_alertImage release];
    [_backgroudButton release];
	[_buttons release];
	[_titleLabel release];
	[_messageLabel release];
	
	//NSLog( @"TSAlertView: TSAlertView dealloc" );
	
    [super dealloc];
}


- (void) TSAlertView_commonInit
{
	self.backgroundColor = [UIColor clearColor];
	self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 
	
	// defaults:
	style = TSAlertViewStyleNormal;
	self.width = 0; // set to default
	self.maxHeight = 0; // set to default
	buttonLayout = TSAlertViewButtonLayoutNormal;
    isDismissByClick = NO;
    //_IsAlertShowing = NO;
	cancelButtonIndex = -1;
	firstOtherButtonIndex = -1;
    _backgroudButton = nil;
    
    //NSLog(@"TSAlertView_commonInit");
}

- (void) setWidth:(CGFloat) w
{
	if ( w <= 0 )
		w = 284;
	
	width = MAX( w, self.backgroundImage.size.width );
}

- (CGFloat) width
{
	if ( nil == self.superview )
		return width;
	
	CGFloat maxWidth = self.superview.bounds.size.width - 20;
	
	return MIN( width, maxWidth );
}

- (void) setMaxHeight:(CGFloat) h
{
	if ( h <= 0 )
		h = 358;
	
	maxHeight = MAX( h, self.backgroundImage.size.height );
}

- (CGFloat) maxHeight
{
	if ( nil == self.superview )
		return maxHeight;
	
	return MAX( maxHeight, self.superview.bounds.size.height - 20 );
}

- (NSMutableArray*) buttons
{
	if ( _buttons == nil )
	{
		_buttons = [[NSMutableArray arrayWithCapacity:4] retain];
	}
	
	return _buttons;
}

- (UILabel*) titleLabel
{
	if ( _titleLabel == nil )
	{
		_titleLabel = [[UILabel alloc] init];
		_titleLabel.font = [UIFont boldSystemFontOfSize: 18];
		_titleLabel.backgroundColor = [UIColor clearColor];
		_titleLabel.textColor = [UIColor whiteColor];
		_titleLabel.textAlignment = UITextAlignmentCenter;
		_titleLabel.lineBreakMode = UILineBreakModeWordWrap;
		_titleLabel.numberOfLines = 0;
	}
	
	return _titleLabel;
}

- (UILabel*) messageLabel
{
	if ( _messageLabel == nil )
	{
		_messageLabel = [[UILabel alloc] init];
		_messageLabel.font = [UIFont systemFontOfSize: 16];
		_messageLabel.backgroundColor = [UIColor clearColor];
		_messageLabel.textColor = [UIColor whiteColor];
		_messageLabel.textAlignment = UITextAlignmentCenter;
		_messageLabel.lineBreakMode = UILineBreakModeWordWrap;
		_messageLabel.numberOfLines = 0;
	}
	
	return _messageLabel;
}

- (UIButton*) backgroudButton
{
    if (_backgroudButton == nil) {
        _backgroudButton = [[NCBarButton alloc] init];
        _backgroudButton.backgroundColor = [UIColor clearColor];
    }
    return _backgroudButton;
}

- (UIImageView*) alertImage
{
    if ( _alertImage == nil ) 
    {
        _alertImage = [[UIImageView alloc] init ];
        _alertImage.image = [UIImage imageNamed:@"image_failureface_small.png"];
        CGRect rect = _alertImage.frame;
        rect.size.width = _alertImage.image.size.width;
        rect.size.height = _alertImage.image.size.height;
        _alertImage.frame = rect;
		_alertImage.userInteractionEnabled = NO;
	}
	return _alertImage;
}

- (UIImage*) backgroundImage
{
	if ( _backgroundImage == nil )
	{
		self.backgroundImage = [[UIImage imageNamed: @"image_inforbox.png"] stretchableImageWithLeftCapWidth: 15 topCapHeight: 30];
	}
	
	return _backgroundImage;
}

- (void) setTitle:(NSString *)t
{
	self.titleLabel.text = t;
}

- (NSString*) title 
{
	return self.titleLabel.text;
}

- (void) setMessage:(NSString *)t
{
	self.messageLabel.text = t;
}

- (NSString*) message  
{
	return self.messageLabel.text;
}

- (NSInteger) numberOfButtons
{
	return [self.buttons count];
}

- (void) setCancelButtonIndex:(NSInteger)buttonIndex
{
	// avoid a NSRange exception
	if ( buttonIndex < 0 || buttonIndex >= [self.buttons count] )
		return;
	
	cancelButtonIndex = buttonIndex;
	
	UIButton* b = [self.buttons objectAtIndex: buttonIndex];
	
	UIImage* buttonBgNormal = [UIImage imageNamed: @"TSAlertViewButtonBackground.png"];
	buttonBgNormal = [buttonBgNormal stretchableImageWithLeftCapWidth: buttonBgNormal.size.width / 2.0 topCapHeight: buttonBgNormal.size.height / 2.0];
	[b setBackgroundImage: buttonBgNormal forState: UIControlStateNormal];
	
	UIImage* buttonBgPressed = [UIImage imageNamed: @"TSAlertViewButtonBackground_Highlighted.png"];
	buttonBgPressed = [buttonBgPressed stretchableImageWithLeftCapWidth: buttonBgPressed.size.width / 2.0 topCapHeight: buttonBgPressed.size.height / 2.0];
	[b setBackgroundImage: buttonBgPressed forState: UIControlStateHighlighted];
}

- (BOOL) isVisible
{
	return self.superview != nil;
}

- (NSInteger) addButtonWithTitle: (NSString *) t
{
	UIButton* b = [UIButton buttonWithType: UIButtonTypeCustom];
	[b setTitle: t forState: UIControlStateNormal];
	
	UIImage* buttonBgNormal = [UIImage imageNamed: @"TSAlertViewButtonBackground.png"];
	buttonBgNormal = [buttonBgNormal stretchableImageWithLeftCapWidth: buttonBgNormal.size.width / 2.0 topCapHeight: buttonBgNormal.size.height / 2.0];
	[b setBackgroundImage: buttonBgNormal forState: UIControlStateNormal];
	
	UIImage* buttonBgPressed = [UIImage imageNamed: @"TSAlertViewButtonBackground_Highlighted.png"];
	buttonBgPressed = [buttonBgPressed stretchableImageWithLeftCapWidth: buttonBgPressed.size.width / 2.0 topCapHeight: buttonBgPressed.size.height / 2.0];
	[b setBackgroundImage: buttonBgPressed forState: UIControlStateHighlighted];
	
	[b addTarget: self action: @selector(onButtonPress:) forControlEvents: UIControlEventTouchUpInside];
	
	[self.buttons addObject: b];
	
	[self setNeedsLayout];
	
	return self.buttons.count-1;
}

- (NSString *) buttonTitleAtIndex:(NSInteger)buttonIndex
{
	// avoid a NSRange exception
	if ( buttonIndex < 0 || buttonIndex >= [self.buttons count] )
		return nil;
	
	UIButton* b = [self.buttons objectAtIndex: buttonIndex];
	
	return [b titleForState: UIControlStateNormal];
}

- (void) dismissWithClickedButtonIndex: (NSInteger)buttonIndex animated: (BOOL) animated
{	
	if ( buttonIndex && [self.delegate respondsToSelector: @selector(alertView:willDismissWithButtonIndex:)] )
	{
		[self.delegate alertView: self willDismissWithButtonIndex: buttonIndex ];
	}
    	
	if ( animated )
	{
		self.window.backgroundColor = [UIColor clearColor];
		self.window.alpha = 1;
		
		[UIView animateWithDuration: 0.2 
						 animations: ^{
							 [self.window resignKeyWindow];
							 self.window.alpha = 0;
						 }
						 completion: ^(BOOL finished) {
							 [self releaseWindow: buttonIndex];
						 }];
		
		[UIView commitAnimations];
	}
	else
	{
		[self.window resignKeyWindow];
		
		[self releaseWindow: buttonIndex];
	}
}
/*
- (void) dismissFromBottom
{
    CGRect rect = self.frame;

    if ( rect.origin.y < 480 ) 
    {
        rect.origin.y = rect.origin.y + 20;
        self.frame = rect;
        [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(dismissFromBottom) userInfo:nil repeats:NO];        
    }
    else
    {
        [self dismissWithClickedButtonIndex:0 animated:NO];
    }
}
*/
- (void) releaseWindow: (int) buttonIndex
{
	if ( [self.delegate respondsToSelector: @selector(alertView:didDismissWithButtonIndex:)] )
	{
		[self.delegate alertView: self didDismissWithButtonIndex: buttonIndex ];
	}
	
	// the one place we release the window we allocated in "show"
	// this will propogate releases to us (TSAlertView), and our TSAlertViewController
	
	//[self.window release];
    RCAppDelegate.tsAlertOverlayWindow.alpha = 0.0;
}

- (void)show
{
    
	[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate date]];
	/*
	TSAlertViewController* avc = [[[TSAlertViewController alloc] init] autorelease];
	avc.view.backgroundColor = [UIColor clearColor];
	
	// $important - the window is released only when the user clicks an alert view button
	TSAlertOverlayWindow* ow = [[TSAlertOverlayWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
	ow.alpha = 0.0;
	ow.backgroundColor = [UIColor clearColor];
	ow.rootViewController = avc;
	[ow makeKeyAndVisible];
	
    
	// fade in the window
	[UIView animateWithDuration: 0.2 animations: ^{
		ow.alpha = 1;
	}];
	
	// add and pulse the alertview
	// add the alertview
	[avc.view addSubview: self];
	[self sizeToFit];
	self.center = CGPointMake( CGRectGetMidX( avc.view.bounds ), CGRectGetMidY( avc.view.bounds ) );;
	self.frame = CGRectIntegral( self.frame );
	//[self pulse];
	
//	if ( self.style == TSAlertViewStyleInput )
//	{
//		[self layoutSubviews];
//	}
    */
    [RCAppDelegate.tsAlertOverlayWindow makeKeyAndVisible];
    // fade in the window
	[UIView animateWithDuration: 0.2 animations: ^{
		RCAppDelegate.tsAlertOverlayWindow.alpha = 1;
	}];
    [RCAppDelegate.tsAlertViewController.view addSubview: self];
	[self sizeToFit];
	self.center = CGPointMake( CGRectGetMidX( RCAppDelegate.tsAlertViewController.view.bounds ), CGRectGetMidY( RCAppDelegate.tsAlertViewController.view.bounds ) );;
	self.frame = CGRectIntegral( self.frame );
}

- (void) pulse
{
	// pulse animation thanks to:  http://delackner.com/blog/2009/12/mimicking-uialertviews-animated-transition/
    self.transform = CGAffineTransformMakeScale(0.6, 0.6);
	[UIView animateWithDuration: 0.2 
					 animations: ^{
						 self.transform = CGAffineTransformMakeScale(1.1, 1.1);
					 }
					 completion: ^(BOOL finished){
						 [UIView animateWithDuration:1.0/15.0
										  animations: ^{
											  self.transform = CGAffineTransformMakeScale(0.9, 0.9);
										  }
										  completion: ^(BOOL finished){
											  [UIView animateWithDuration:1.0/7.5
															   animations: ^{
																   self.transform = CGAffineTransformIdentity;
															   }];
										  }];
					 }];
	
}

- (void) onButtonPress: (id) sender
{
	int buttonIndex = [_buttons indexOfObjectIdenticalTo: sender];
	
	if ( [self.delegate respondsToSelector: @selector(alertView:clickedButtonAtIndex:)] )
	{
		[self.delegate alertView: self clickedButtonAtIndex: buttonIndex ];
	}
	
	if ( buttonIndex == self.cancelButtonIndex )
	{
		if ( [self.delegate respondsToSelector: @selector(alertViewCancel:)] )
		{
			[self.delegate alertViewCancel: self ];
		}	
	}
	
	[self dismissWithClickedButtonIndex: buttonIndex  animated: YES];
}

- (CGSize) recalcSizeAndLayout: (BOOL) layout
{
	BOOL	stacked = !(self.buttonLayout == TSAlertViewButtonLayoutNormal && [self.buttons count] == 2 );
	
	CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2);
	
	CGSize  titleLabelSize = [self titleLabelSize];
    CGSize  imageOnAlertSize = [self imageOnAlertSize];
	CGSize  messageViewSize = [self messageLabelSize];
	CGSize  buttonsAreaSize = stacked ? [self buttonsAreaSize_Stacked] : [self buttonsAreaSize_SideBySide];
	
	CGFloat totalHeight = kTSAlertView_TopMargin + titleLabelSize.height + kTSAlertView_RowMargin + imageOnAlertSize.height + messageViewSize.height + kTSAlertView_RowMargin + buttonsAreaSize.height + kTSAlertView_BottomMargin;
	
	if ( layout )
	{
		// title
		CGFloat y = kTSAlertView_TopMargin;
		if ( self.title != nil )
		{
			self.titleLabel.frame = CGRectMake( kTSAlertView_LeftMargin, y, titleLabelSize.width, titleLabelSize.height );
			[self addSubview: self.titleLabel];
			y += titleLabelSize.height + kTSAlertView_RowMargin;
		}
		
        // image on alert
        if ( self.alertImage != nil && self.alertImage.image != nil) 
        {
            CGRect aRect = self.alertImage.frame;
            aRect.origin.x = (self.frame.size.width - aRect.size.width)/2;
            y = y + 10;
            aRect.origin.y = y;
            self.alertImage.frame = aRect;
            [self addSubview: self.alertImage];
            y += 50 + kTSAlertView_RowMargin;
        }
		// message
		if ( self.message != nil )
		{
            if (self.alertImage != nil && self.alertImage.image != nil) 
            {
                CGRect rect = self.messageLabel.frame;
                rect.origin.x = (self.frame.size.width - messageViewSize.width)/2;
                rect.origin.y = y;
                rect.size.width = messageViewSize.width;
                rect.size.height = messageViewSize.height;
                self.messageLabel.frame = rect;
                [self addSubview: self.messageLabel];
                y += messageViewSize.height + kTSAlertView_RowMargin;
            }
            else
            {
                CGRect rect = self.messageLabel.frame;
                rect.origin.x = (self.frame.size.width - messageViewSize.width)/2;
                rect.origin.y = (self.frame.size.height - messageViewSize.height)/2;
                rect.size.width = messageViewSize.width;
                rect.size.height = messageViewSize.height;
                self.messageLabel.frame = rect;

                [self addSubview: self.messageLabel];
                y += messageViewSize.height + kTSAlertView_RowMargin;
            }
            
        }
		
		// buttons
        if ([self.buttons count]!=0) 
        {
            CGFloat buttonHeight = [[self.buttons objectAtIndex:0] sizeThatFits: CGSizeZero].height;
            if ( stacked )
            {
                CGFloat buttonWidth = maxWidth;
                for ( UIButton* b in self.buttons )
                {
                    b.frame = CGRectMake( kTSAlertView_LeftMargin, y, buttonWidth, buttonHeight );
                    [self addSubview: b];
                    y += buttonHeight + kTSAlertView_RowMargin;
                }
            }
            else
            {
                CGFloat buttonWidth = (maxWidth - kTSAlertView_ColumnMargin) / 2.0;
                CGFloat x = kTSAlertView_LeftMargin;
                for ( UIButton* b in self.buttons )
                {
                    b.frame = CGRectMake( x, y, buttonWidth, buttonHeight );
                    [self addSubview: b];
                    x += buttonWidth + kTSAlertView_ColumnMargin;
                }
            }
        }
		
	}
    
    if ( totalHeight < self.backgroundImage.size.height ) 
    {
        totalHeight = self.backgroundImage.size.height;
    }
    if (self.width > self.backgroundImage.size.width) 
    {
        self.width = self.backgroundImage.size.width;
    }
    
    {
        //NSLog(@"%f %f", self.width, totalHeight);
    }
    if ( self.isDismissByClick &&( nil != [self backgroudButton])) 
    {
        
        [_backgroudButton addTarget:self action:@selector(dismissByClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_backgroudButton];
         _backgroudButton.frame = CGRectMake( kTSAlertView_LeftMargin, kTSAlertView_TopMargin, self.width, totalHeight);
    }
	return CGSizeMake( self.width, totalHeight );
}

- (void)dismissByClick
{
    [self dismissWithClickedButtonIndex:1 animated:NO];
    self.delegate = nil;
}

- (CGSize) titleLabelSize
{
	CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2);
	CGSize s = [self.titleLabel.text sizeWithFont: self.titleLabel.font constrainedToSize: CGSizeMake(maxWidth, 1000) lineBreakMode: self.titleLabel.lineBreakMode];
	if ( s.width < maxWidth )
		s.width = maxWidth;
	
	return s;
}

- (CGSize) imageOnAlertSize
{
    return CGSizeMake(_alertImage.frame.size.width, _alertImage.frame.size.height);
}

- (CGSize) messageLabelSize
{
	CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2) - 30;
	CGSize s = [self.messageLabel.text sizeWithFont: self.messageLabel.font constrainedToSize: CGSizeMake(maxWidth, 1000) lineBreakMode: self.messageLabel.lineBreakMode];
	if ( s.width < maxWidth )
		s.width = maxWidth;
	[self.messageLabel sizeToFit];
	return s;
}

- (CGSize) buttonsAreaSize_SideBySide
{
	CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2);
	
	CGSize bs = [[self.buttons objectAtIndex:0] sizeThatFits: CGSizeZero];
	
	bs.width = maxWidth;
	
	return bs;
}

- (CGSize) buttonsAreaSize_Stacked
{
	CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2);
	int buttonCount = [self.buttons count];
	
    if (buttonCount!=0) 
    {
        CGSize bs = [[self.buttons objectAtIndex:0] sizeThatFits: CGSizeZero];
	
        bs.width = maxWidth;
	
        bs.height = (bs.height * buttonCount) + (kTSAlertView_RowMargin * (buttonCount-1));
        
        return bs;
    }
	else
        
        return CGSizeMake(0, 0);
}

@end




